﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CARMSBusiness.InterFace;
using CARMSData.CollectionClasses;
using CARMSData.EntityClasses;
using CARMSData.HelperClasses;
using SD.LLBLGen.Pro.ORMSupportClasses;

namespace CARMSBusiness
{
    public class MessageBusiness
    { 
        #region  旧的思路
        /// <summary>
        /// 只发送 ReportCategoryToTemplate 配置表中的MessageActionType 为 SendMessage类型的消息
        /// </summary>
        /// <param name="reportHeaderID"></param>
        /// <param name="buttonEventID"></param>
        /// <returns></returns>
       public static bool SendMessageByReportID(int reportHeaderID,int buttonEventID)
       {
           var reportHeader = new ReportHeaderEntity(reportHeaderID);

           var collection = new TemplateCollection();
           var relation = new RelationCollection();
           relation.Add(TemplateEntity.Relations.ReportCategoryToTemplateEntityUsingTemplateId);

            var condition = new PredicateExpression();
            condition.Add(ReportCategoryToTemplateFields.ReportCategoryId == reportHeader.ReportCategoryId &
                               ReportCategoryToTemplateFields.ButtonEventId == buttonEventID 
                               & ReportCategoryToTemplateFields.ActionType == DictionaryCategoryConst.MessageActionType.SendMessage);
            collection.GetMulti(condition, relation);



           foreach (var template in collection)
           {
               var message = new MessageEntity
                                 {

                                     TemplateId = template.TemplateId,
                                     Subject = template.TemplateSubject,
                                     Content = template.TemplateContent,
                                     TargetUserId = reportHeader.TargetUserId,
                                     ReportHeaderId = reportHeader.ReportHeaderId,
                                     OriginButtonEventId = buttonEventID

                                 };
               message.Save();  
           }

           return true;
           
       }


        //public static  MessageCollection GetMessageListByUserID(int approverUserID)
        //{
        //    var collection = new MessageCollection();
        //    collection.GetMulti(MessageFields.TargetUserId == approverUserID);
        //    return collection;
        //}

        /// <summary>
        /// delete 除了当前button event 的所有message, 
       /// 只删除 ReportCategoryToTemplate 配置表中的MessageActionType 为 DeleteMessage 类型的消息
        /// </summary>
        /// <param name="reportHeaderID"></param>
        /// <returns></returns>
       public static bool DeleteMessageByReportHeaderID(int reportHeaderID, int buttonEventID)
        {

            var reportHeader = new ReportHeaderEntity(reportHeaderID);

           //删除类型的template 列表
            var rptCategoryToTempCollection = new ReportCategoryToTemplateCollection();
            var tmpCondition = new PredicateExpression();
            tmpCondition.Add(ReportCategoryToTemplateFields.ReportCategoryId ==
                                                 reportHeader.ReportCategoryId &
                                                 ReportCategoryToTemplateFields.ButtonEventId == buttonEventID
                                                 &
                                                 ReportCategoryToTemplateFields.ActionType ==
                                                 DictionaryCategoryConst.MessageActionType.DeleteMessage);

       
            rptCategoryToTempCollection.GetMulti(tmpCondition);
            var tmpList = from item in rptCategoryToTempCollection select item.TemplateId;


            var collection = new MessageCollection();
            var condition = new PredicateExpression();
            condition.Add(MessageFields.ReportHeaderId == reportHeaderID);
            switch (buttonEventID)
            {
                case DictionaryCategoryConst.ButtonPermission.Add:
                    condition.Add(MessageFields.OriginButtonEventId == DictionaryCategoryConst.ButtonPermission.Reject);
                    break;
                case DictionaryCategoryConst.ButtonPermission.Confirm:
                    condition.Add(MessageFields.OriginButtonEventId == DictionaryCategoryConst.ButtonPermission.Add);
                    break;
                case DictionaryCategoryConst.ButtonPermission.Reject:
                    condition.Add(MessageFields.OriginButtonEventId == DictionaryCategoryConst.ButtonPermission.Add);
                    break;
            }
            //删除指定类型的message
            var deleteTemplateField = new FieldCompareRangePredicate(MessageFields.TemplateId, false, tmpList);
            condition.Add(deleteTemplateField);

       

            collection.UpdateMulti(new MessageEntity
                                       {
                                           IsActive = false
                                       }, condition);
            return true;
        }

        #endregion


        #region 使用G表流程思路

   
        #endregion

    }
}
